﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Web.Services;

public partial class collectionPage : System.Web.UI.Page
{
    CommonPage _comment = new CommonPage();
    VLGDataContext _dataContext = new VLGDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["currentItem"] = "yoursp";
        if (!IsPostBack)
        {
            Page.Title = "Trang cá nhân | Bộ sưu tập của bạn";
            try
            {
                if (String.IsNullOrEmpty(Session["KhachHang"].ToString()))
                {
                    Response.Redirect(ResolveClientUrl("warnLogin.aspx"));
                }
                else
                {
                    fillDulieuLenGrid();
                }
            }
            catch { Response.Redirect(ResolveClientUrl("Default.aspx")); }
        }
    }
    //protected void AspNetPager1_PageChanged(object src, EventArgs e)
    //{
    //    fillDulieuLenGrid();
    //}
    void fillDulieuLenGrid()
    {

        var gigCanhan = from c in _dataContext.Collections
                        where c.idAcc == _comment.Customer.idAcc
                        orderby c.TimeInput descending
                        select c;

        GridView1.DataSource = gigCanhan;
        GridView1.DataBind();
       
    }

    protected void btnTiep2_Click(object sender, EventArgs e)
    {
        string noidung = TextBox1.Text.ToString().Trim();
        Response.Redirect(ResolveClientUrl("~/qlcongviec.aspx?op=add&to=") + noidung);
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="collectionName"></param>
    /// <returns>
    /// 0 : lỗi
    /// 1 : lỗi đã tồn tại
    /// 2 : thành công
    /// </returns>
    [WebMethod]
    public static int InsertCollectionModal(string collectionName)
    {
        if (string.IsNullOrEmpty(collectionName))
            return 0;

        else
        {
            try
            {
                VLGDataContext _dataContext = new VLGDataContext();
                CommonPage _comment = new CommonPage();
                var con = from c in _dataContext.Collections
                          where c.Name.Trim().ToLower().Equals(collectionName.Trim().ToLower()) && c.idAcc == _comment.Customer.idAcc
                          select c;
                if (con != null && con.Count() > 0)
                { 
                    
                    return -1;
                
                }
                else
                {
                    Collection collection = new Collection();
                    collection.Name = collectionName;
                    collection.TimeInput = DateTime.Now;
                    collection.idAcc = _comment.Customer.idAcc;
                    _dataContext.Collections.InsertOnSubmit(collection);
                    _dataContext.SubmitChanges();
                    return collection.idCollection;

                }
            }
            catch
            {
                return 0;
            }
        }

    }
    [WebMethod]
    public static int InsertCollectionDetail(string idCollection, string idGig)
    {
        if (string.IsNullOrEmpty(idCollection) || string.IsNullOrEmpty(idGig))
            return 0;

        else
        {
            try
            {
                VLGDataContext _dataContext = new VLGDataContext();
                CommonPage _comment = new CommonPage();
                CollectionDetail collection = new CollectionDetail();
                collection.idGig = Convert.ToInt32(idGig);
                collection.TimeInput = DateTime.Now;
                collection.idAcc = _comment.Customer.idAcc;
                collection.idCollection = Convert.ToInt32(idCollection);
                _dataContext.CollectionDetails.InsertOnSubmit(collection);
                _dataContext.SubmitChanges();
                return collection.idCollectiondDetail;
            }
            catch
            {
                return 0;
            }
        }

    }
    [WebMethod]
    public static int DeleteCollection(string id)
    {

        if (!string.IsNullOrEmpty(id))
        {
            try
            {
                VLGDataContext _dataContext = new VLGDataContext();
                CommonPage _comment = new CommonPage();
                var idInt = Convert.ToInt32(id);
                var collectionDetail = _dataContext.CollectionDetails.Select(p => p).Where(p => p.idCollection == idInt);
                _dataContext.CollectionDetails.DeleteAllOnSubmit(collectionDetail);
                var collection = _dataContext.Collections.SingleOrDefault(c => c.idCollection == idInt);
                _dataContext.Collections.DeleteOnSubmit(collection);
                _dataContext.SubmitChanges();
                return 1;

            }
            catch
            {

                return 0;
            }

        }
        else return 0;
    }
    [WebMethod]
    public static CollectionJson SelectCollectionModal(string id)
    {
        if (string.IsNullOrEmpty(id))
            return null;

        else
        {
            try
            {
                VLGDataContext _dataContext = new VLGDataContext();
                var collection = (from c in _dataContext.Collections
                                  where c.idCollection == Convert.ToInt32(id)
                                  select new CollectionJson
                                  {
                                      Name = c.Name,
                                      TimeInput = string.Format("{0:dd/MM/yyyy}", c.TimeInput),
                                      Id = c.idCollection
                                  }).Single();


                return collection;
            }
            catch
            {
                return null;
            }
        }

    }

    /// <summary>
    /// Luu gig vao bo suu tap dua vao ten bo suu tap
    /// Json tra ve ten bo suu tap
    /// </summary>
    /// <param name="nameCollection"></param>
    /// <param name="idGig"></param>
    /// <returns>
    /// 0 : lỗi
    /// 1 : lỗi đã tồn tại
    /// 2 : thành công
    /// </returns>
    [WebMethod]
    public static int InsertCollectionDetails(string nameCollection, string idGig)
    {
        if (string.IsNullOrEmpty(nameCollection) || string.IsNullOrEmpty(idGig))
            return 0;
        else
        {
            try
            {

                VLGDataContext _dataContext = new VLGDataContext();
                CommonPage _comment = new CommonPage();
                var con = (from c in _dataContext.Collections
                           where c.Name.ToLower().Equals(nameCollection.ToLower())
                           select c).Single();
                if (con != null)
                {
                    var conCheck = from cc in _dataContext.CollectionDetails
                                   where cc.idGig == Convert.ToInt32(idGig) && cc.idCollection == con.idCollection
                                   select cc;
                    //cong việc đã tồn tại trong bộ sưu tập
                    if (conCheck != null && conCheck.Count() > 0)
                    {
                        return 1;
                    }
                    else
                    {
                        CollectionDetail collection = new CollectionDetail();
                        collection.idGig = Convert.ToInt32(idGig);
                        collection.TimeInput = DateTime.Now;
                        collection.idAcc = _comment.Customer.idAcc;
                        collection.idCollection = con.idCollection;
                        _dataContext.CollectionDetails.InsertOnSubmit(collection);
                        _dataContext.SubmitChanges();
                        return 2;
                    }
                }
                else return 0;
            }
            catch
            {
                return 0;
            }
        }

    }
}

































